package interview.combineintervals;

public class Interval implements Comparable<Interval> {
	double lower;
	double higher;
	
	public double getLower() {
		return lower;
	}
	public void setLower(double lower) {
		this.lower = lower;
	}
	public double getHigher() {
		return higher;
	}
	public void setHigher(double higher) {
		this.higher = higher;
	}
	
	public Interval(double low, double high) {
		lower = low;
		higher = high;
	}

	@Override
	public int compareTo(Interval interval) {
		if(lower < interval.getLower()) {
			if (higher < interval.getLower()) {
				return -1;
			} else {
				return 0;
			}
		} else if (lower == interval.getLower()) {
			return 0;
		} else {
			if (lower > interval.getHigher()) {
				return 1;
			} else {
				return 0;
			}
		}
	}
	
	public Interval combine(Interval interval) {
		if (interval.compareTo(this) == 0) {
			return new Interval(Math.min(lower, interval.getLower()), Math.max(higher, interval.getHigher()));
		} else {
			return null;
		}
	}
	
	@Override
	public String toString() {
		return "[" + lower + "," + higher + "]";
	}
}
